<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="560px">
      <el-row>
        <el-form ref="form" label-width="80px">
          <el-form-item label="模型标题">
            <el-input placeholder="请输入模型标题" v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="模型名称">
            <el-input placeholder="请输入模型名称" v-model="form.name" :disabled="isedit"></el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="onSubmit">{{ isedit ? '确认修改' : '确认添加' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import dialogMixin from '@/mixins/dialogMixin.js';
import { addModel, editModel } from '@/api/system/eventModel';

export default {
  name: 'ModeleEdit',
  mixins: [dialogMixin],
  components: {},
  data() {
    return {
      title: '编辑模型信息',
      form: {
        title: '',
        name: ''
      },
      isedit: false
    }
  },
  mounted() {
  },
  methods: {
    add() {
      this.reset();
      this.show();
    },
    edit(data) {
      this.reset();
      this.form = Object.assign({}, data);
      this.isedit = true;
      this.show();
    },
    reset() {
      this.form = {
        title: '',
        name: ''
      };
      this.isedit = false;
    },
    onSubmit() {
      if (this.isedit) {
        editModel(this.form).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess('模型修改成功');
            this.close();
            this.$emit('handleOk');
          } else {
            this.$modal.msgError('模型修改失败');
          }
        })
      } else {
        addModel(this.form).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess('模型添加成功');
            this.close();
            this.$emit('handleOk');
          } else {
            this.$modal.msgError('模型添加失败');
          }
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped></style>